﻿Public Class frmReservations
    Private szSQL As String
    Dim szCritere As String
    Private rs As ADODB.Recordset
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Private Sub frmReservations_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        InitListeEleves(0)
        TC_RESA.SelectedTab = TabPage1
        BO_NOTIFIER.Visible = False

    End Sub

    Private Sub InitListeEleves(ByVal ix As Integer)

        Dim nb As Integer = 0

        Me.Cursor = Cursors.WaitCursor
        ' effacer la listview
        LV_ELEVES.Items.Clear()

        szSQL = "CALL spGET_RESERVATIONS()"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_ELEVES.Items.Add(rs.Fields("ref").Value)
            LV_ELEVES.Items(nb).SubItems.Add(IfDate(rs.Fields("date").Value))
            LV_ELEVES.Items(nb).SubItems.Add(IfDate(rs.Fields("fin_reservation").Value))
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("cd_cli").Value)
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("nompren").Value)
            LV_ELEVES.Items(nb).SubItems.Add(Zero2Space(rs.Fields("solde_reel").Value))
            LV_ELEVES.Items(nb).SubItems.Add(Zero2Space(rs.Fields("solde_prev").Value))
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("no_ligne").Value)
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("noplan").Value)
            If rs.Fields("solde_reel").Value < 0 Then
                LV_ELEVES.Items(nb).ForeColor = Color.Red
            End If
            ' Get the next record
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()
        Me.Cursor = Cursors.Default

        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            SortListView(LV_ELEVES, m_SortingColumn, m_SortingIndex)
        End If

        'positionner le curseur sur la première ligne
        setIndice(ix, LV_ELEVES)

    End Sub

    Private Sub LV_ELEVES_ColumnClick(sender As Object, e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_ELEVES.ColumnClick
        SortListView(LV_ELEVES, m_SortingColumn, e.Column)
        m_SortingIndex = e.Column
    End Sub


    Private Sub LV_ELEVES_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_ELEVES.DoubleClick

        If LV_ELEVES.SelectedItems.Count > 0 Then
            ' afficher les détails de la ligne NoLigne
            frmResaLigne.pNoLigne = LV_ELEVES.SelectedItems(0).SubItems(7).Text
            frmResaLigne.ShowDialog()

            InitListeEleves(LV_ELEVES.SelectedItems(0).Index)
        End If

    End Sub

    Private Sub LV_ELEVES_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_ELEVES.KeyDown

        Select Case e.KeyCode
            Case Keys.Enter  ' Enter = Double-Clic
                LV_ELEVES_DoubleClick(sender, e)

        End Select

    End Sub

    Private Sub BO_CONFIRMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_CONFIRMER.Click

        If MessageBox.Show("Ce traitement va parcourir les réservations par date croissante :" & vbCrLf & _
                  "- Si la date de fin résa est dépassée, la résa sera annulée" & vbCrLf & _
                  "- Sinon, la résa sera confirmée si le solde de l'élève le permet." & vbCrLf & _
                  "Puis un email de notification sera envoyé à l'élève.", "Confirmation des Résa", _
                  MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then

            szSQL = "CALL spUPD_RESERVATIONS()"
            ExecQuery(ctDSN_AEM, szSQL)

            ' ré-affiche la liste des résa
            InitListeEleves(0)

        End If


    End Sub

    Private Sub TC_RESA_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TC_RESA.SelectedIndexChanged
        If TC_RESA.SelectedIndex = 1 Then
            InitListeTraitees()
            BO_NOTIFIER.Visible = True
        Else
            BO_NOTIFIER.Visible = False
        End If
    End Sub

    Private Sub InitListeTraitees()

        Dim nb As Integer = 0

        Me.Cursor = Cursors.WaitCursor
        ' effacer la listview
        LV_TRAITEES.Items.Clear()

        szSQL = "SELECT * FROM email_resa ORDER BY no_id DESC LIMIT 100"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_TRAITEES.Items.Add(rs.Fields("ref").Value)
            LV_TRAITEES.Items(nb).SubItems.Add(IfDatetime(rs.Fields("resa_date").Value))
            LV_TRAITEES.Items(nb).SubItems.Add(rs.Fields("cd_cli").Value)
            LV_TRAITEES.Items(nb).SubItems.Add(rs.Fields("nompren").Value)
            LV_TRAITEES.Items(nb).SubItems.Add(rs.Fields("resa_statut").Value)
            LV_TRAITEES.Items(nb).SubItems.Add(IfDatetime(rs.Fields("envoye_le").Value))
            LV_TRAITEES.Items(nb).SubItems.Add(rs.Fields("cree_le").Value)
            ' Get the next record
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()
        Me.Cursor = Cursors.Default

    End Sub

    Private Sub BO_NOTIFIER_Click(sender As System.Object, e As System.EventArgs) Handles BO_NOTIFIER.Click

        Me.Cursor = Cursors.WaitCursor
        Notifier_email()
        Me.Cursor = Cursors.Default

        InitListeTraitees()

    End Sub

End Class
